home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / file-tra / fsp-2.7 / fsp-2 / fsp / clients / fgetcmd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-21  |  5.4 KB  |  238 lines

  1.     /*********************************************************************\
  2.     *  Copyright (c) 1991 by Wen-King Su (wen-king@vlsi.cs.caltech.edu)   *
  3.     *                                                                     *
  4.     *  You may copy or modify this file in any manner you wish, provided  *
  5.     *  that this notice is always included, and that you hold the author  *
  6.     *  harmless for any loss or damage resulting from the installation or *
  7.     *  use of this software.                                              *
  8.     \*********************************************************************/
  9.  
  10. #include "tweak.h"
  11. #include "client_def.h"
  12. #include "c_extern.h"
  13. #include "bsd_extern.h"
  14. #include <signal.h>
  15.  
  16. int get_clobbertype=CLOBBERONFIND;
  17. char *fname;
  18. int optletter;
  19. int suffix;
  20. char *tname;
  21. unsigned long start_from;
  22. extern int optind;
  23.  
  24. static int len;
  25.  
  26. static void fsp_cleanup PROTO1(int, signum)
  27. {
  28.   char filename[20];
  29. #ifndef VMS
  30.   sprintf(filename,".fsp.%d",getpid());
  31. #else /* no more than one dot allowed in filenames */
  32.   sprintf(filename,"fsp_%d",getpid());
  33. #endif
  34.   unlink(filename);
  35.   exit(1);
  36. }
  37.  
  38. static int get_file PROTO4(char *, path, struct stat *, sbufp, int, mode, int, level)
  39. {
  40.   char *name = path + len;
  41. #ifdef VMS
  42.   char filename[255];
  43. #endif
  44.   FILE *fp;
  45.   struct stat statbuf;
  46.   
  47. #ifdef VMS
  48.   /* convert more than one dot to underscores. */
  49.   strcpy(filename, name); name=filename;
  50.   convdots(name, filename);
  51. #endif
  52.   
  53.   if (get_clobbertype==NOCLOBBER) {
  54.     if (fp=fopen(name,"r")) {
  55.       fclose(fp);
  56.       fprintf(stderr,"Will not overwrite existing file %s\n",name);
  57.       return;
  58.     }
  59.   }
  60.   
  61.   if (get_clobbertype==UNIQUE) {
  62.     fname=name;
  63.     name=(char *)malloc(strlen(fname)+5);
  64.     strcpy(name,fname);
  65.     for (suffix=0 ; (fp=fopen(name,"r")) ; suffix++) {
  66.       fclose(fp);
  67.       sprintf(name,"%s-%d",fname,suffix);
  68.     }
  69.   }
  70.   
  71.   if (get_clobbertype==CLOBBERONFIND) {
  72.     fname=name;
  73.     name=(char*)malloc(20);
  74. #ifdef VMS /* no more than one dot allowed in filenames */
  75.     sprintf(name,"fsp_%d",getpid());
  76. #else
  77.     sprintf(name,".fsp.%d",getpid());
  78. #endif
  79.   }
  80.   
  81.   if(get_clobbertype == APPEND)  {
  82.     if(stat(name, &statbuf) == 0) {
  83.       start_from = statbuf.st_size;
  84.       if((fp = fopen(name,"a")) == NULL) perror(name);
  85.     } else start_from = -1;
  86.   } else start_from = -1;
  87.   
  88.   if(start_from == -1) {
  89.     fp = fopen(name, "w");
  90.     start_from = 0;
  91.   }
  92.   
  93.   if(fp) {
  94.     if(util_download(path,fp, start_from) == -1) {
  95.       fclose(fp);
  96.       unlink(name);
  97.     } else fclose(fp);
  98.   } else fprintf(stderr,"Cannot write %s\n",name);
  99.   
  100.   if (get_clobbertype==CLOBBERONFIND) {
  101.     rename(name,fname);
  102.     free(name);
  103.   }
  104.   
  105.   if (get_clobbertype==UNIQUE) {
  106.     free(name);
  107.   }
  108. }
  109.  
  110. static int make_dir PROTO3(char *, name, struct stat *, sbufp, u_long *, mode)
  111. {
  112.     struct stat sbuf;
  113.  
  114.     if (*mode != RECURSIVE) return (-1);
  115.  
  116.     if (stat(name + len, &sbuf) == 0)
  117.     {
  118.         /* check if the directory already exists... */
  119.         if (S_ISDIR(sbuf.st_mode))
  120.             return (0);
  121.  
  122.         /* the directory doesn't exist, but *something* does... urgh! */
  123.         fprintf(stderr,"fgetcmd: local file `%s' is not a directory\n",
  124.                  name + len);
  125.         return (-1);
  126.     }
  127.  
  128.     /* nothing exists by this name -- try to create it */
  129.     if (mkdir(name + len, 0755) < 0)
  130.     {
  131.         perror (name + len);
  132.         return(-1);
  133.     }
  134.  
  135.     return (0);
  136. }
  137.  
  138. int main PROTO3(int, argc, char **, argv, char **, envp)
  139. {
  140.   char **av, *av2[2], n[1024];
  141.   int prompt, mode = 0;
  142.   
  143.   signal(SIGHUP,fsp_cleanup);
  144.   signal(SIGINT,fsp_cleanup);
  145.   signal(SIGQUIT,fsp_cleanup);
  146.   signal(SIGILL,fsp_cleanup);
  147.   signal(SIGTRAP,fsp_cleanup);
  148.   signal(SIGFPE,fsp_cleanup);
  149.   signal(SIGSEGV,fsp_cleanup);
  150. #ifndef __linux__
  151.   signal(SIGEMT,fsp_cleanup);
  152.   signal(SIGBUS,fsp_cleanup);
  153.   signal(SIGSYS,fsp_cleanup);
  154. #endif
  155.   signal(SIGPIPE,fsp_cleanup);
  156.   signal(SIGTERM,fsp_cleanup);
  157.  
  158.   env_client();
  159.   if (strcmp(env_local_dir,".") && chdir(env_local_dir)) {
  160.     perror("chdir");
  161.     exit(1);
  162.   }
  163.   
  164.   /* Parse options
  165.    * -f    forces overwrite
  166.    * -u    forces unique names
  167.    * -t    uses temporary file to download
  168.    * -n    forces noclobber
  169.    * -a   append to files if they exist
  170.    * -r   recursively get directories 
  171.    */
  172.   while ((optletter=getopt(argc, argv,"futnar")) != EOF)
  173.     switch (optletter) {
  174.       case 'f':
  175.         get_clobbertype=CLOBBER;
  176.     break;
  177.       case 'u':
  178.     get_clobbertype=UNIQUE;
  179.     break;
  180.       case 't':
  181.     get_clobbertype=CLOBBERONFIND;
  182.     break;
  183.       case 'n':
  184.     get_clobbertype=NOCLOBBER;
  185.     break;
  186.       case 'a':
  187.      get_clobbertype = APPEND;
  188.     break;
  189.       case 'r':
  190.     mode=RECURSIVE;
  191.     break;
  192.     }
  193.   
  194.   if(argc > optind) {
  195.     for( ; argc>optind ; optind++) {
  196.       if(!(av = glob(argv[optind])))  {
  197.     av = av2;
  198.     av2[0] = argv[optind];
  199.     av2[1] = 0;
  200.       }
  201.       while(*av)
  202.       {
  203.          for(len = strlen(*av); len >= 0 && (*av)[len] != '/'; len--);
  204.          len++;
  205.          util_process_file(*av, mode, get_file, make_dir, 0L, 0);
  206.          av++;
  207.       }
  208.  
  209.     }
  210.   } else {
  211.     prompt = isatty(0);
  212.     while(1) {
  213.       if(prompt) {
  214.     fputs("fget: ",stdout);
  215.     fflush(stdout);
  216.       }
  217.       if(!gets(n)) break;
  218.       if(!*n) continue;
  219.       if(!(av = glob(n))) {
  220.     av = av2;
  221.     av2[0] = n;
  222.     av2[1] = 0;
  223.       }
  224.       while(*av)
  225.       {
  226.          for(len = strlen(*av); len >= 0 && (*av)[len] != '/'; len--);
  227.          len++;
  228.          util_process_file(*av, mode, get_file, make_dir, 0L, 0);
  229.          av++;
  230.       }
  231.     }
  232.   }
  233.   
  234.   client_done();
  235.   
  236.   exit(0);
  237. }
  238.